# coding: utf8
# datetime: 2021-03-21
# 题目地址: https://leetcode-cn.com/problems/set-matrix-zeroes/


from typing import List


class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        raws, cols = len(matrix), len(matrix[0])
        raw_flag  = matrix[0].count(0)
        col_falg = False
        for raw in range(raws):
            if matrix[raw][0] == 0:
                col_falg = True
                break
        for raw in range(1, raws):
            for col in range(1, cols):
                if matrix[raw][col] == 0:
                    matrix[raw][0] = matrix[0][col] = 0
        
        for raw in range(1, raws):
            if matrix[raw][0] == 0:
                matrix[raw] = [0]*cols
        for col in range(1, cols):
            if matrix[0][col] == 0:
                for raw in range(1, raws):
                    matrix[raw][col] = 0

        if raw_flag:
            matrix[0] = [0]*cols
        if col_falg:
            for raw in range(raws):
                matrix[raw][0] = 0
        return matrix


a = Solution()
print(a.setZeroes([[0,1,2,0],[3,4,5,2],[1,3,1,5]]))